<?php


require_once(dirname(__FILE__).'/../../common/log.php');
require_once(dirname(__FILE__).'/../../common/defines.php');
require_once(dirname(__FILE__).'/../../common/return.php');
require_once(dirname(__FILE__).'/../../database/mysql.class.php');
require_once(dirname(__FILE__).'/../../database/redis.class.php');
require_once(dirname(__FILE__).'/../../utils/HttpUtils.php');

require_once(dirname(__FILE__).'/../common.php');


function set_user_alive_info($type, $userId)
{
    $info['type']    = $type;
    $info['time']    = time();
    $info['userId']  = $userId;
    $ret  = HttpUtils::send_post_request('http://localhost/scoreAward/onlineAward/setAliveTime.php', [], $info);
}

function kefu_login($userId)
{
    $info['time']    = time();
    $info['kefuId']  = $userId;
    $ret  = HttpUtils::send_post_request('http://localhost/kefu/kefuLogin.php', [], $info);
    logd($ret);
}

function kefu_logout($userId)
{
    $info['time']    = time();
    $info['kefuId']  = $userId;
    $ret  = HttpUtils::send_post_request('http://localhost/kefu/kefuLogout.php', [], $info);
    logd($ret);
}

function send_waiting_call($mysql, $userId)
{
    $ret  = $mysql->find('ykj_call_sheet_info', 'type,ucid,earn_price,call_platform_id,answer_platform_id,caller_id,answer_id', 'status='.CALL_STATUS_WAITING.' AND (answer_platform_id='.$userId.')');

    if ($ret === FAILURE)
    {
        loge("send_waiting_call failed: userId = ".$userId);

        return;
    }

    if (empty($ret))
    {
        return;
    }

    $data['ucid']    = $ret['ucid'];
    $data['price']   = $ret['earn_price'];
	$data['type']    = $ret['type'];
	
    $caller  = $mysql->find('ykj_app_user_ext', 'head_url,nick_name', 'id='.$ret['caller_id']);
    $data['fromId']  = $ret['caller_id'];
	$data['toId']    = $ret['answer_id'];
	$data['fromPlatFormId']  = $ret['call_platform_id'];
	$data['toPlatFormId']    = $ret['answer_platform_id'];
	$data['fromNickName']  = $caller['nick_name'];
	$data['fromHeadUrl']   = $caller['head_url'];
	$data['fromNickName']  = $caller['nick_name'];
	
    send_kmsg_message($ret['call_platform_id'], [(int)$ret['answer_platform_id']], MSG_TYPE_CALLOUT, 0, $data, 0);
}

$body  = file_get_contents('php://input');   
$data  = xor_decode($body, $_SERVER['CONTENT_LENGTH']);
logd("recv: ".$data);

$data  = json_decode($data, true);
if ((!isset($data['type'])) || (!is_int($data['type'])) || (!isset($data['data'])) || (!is_array($data['data'])))
{
    loge("params error !");

    failure(ERR_PARAM);
}

switch ($data['type'])
{
    case 100:
	{
        //连接数据库
        $mysql  = new mysqldb();
        $ret    = $mysql->open_db();
        if ($ret === FAILURE)
        {
            exit();
        }

        $userId  = $data['data']['userId'];
        $ret     = $mysql->update('ykj_app_user', ['online_status' => 2, 'recently_access_time' => time()], 'platform_id='.$userId);
        if ($ret === FAILURE)
        {
            loge("update online status failed: platform_id = ".$userId);

            break;
        }

        $ret  = $mysql->find('ykj_app_user', 'account_type,sex,disturb_flag', 'platform_id='.$userId);
        if (($ret === FAILURE) || empty($ret))
        {
            loge("get user base info failed: platform_id = ".$userId);

            break;
        }

        $redis  = new RedisUtils(['select' => 6]);
        $redis->zadd('userOnline', $ret['sex'], $userId);

        if ($ret['account_type'] == ACCOUNT_TYPE_KEFU)
        {
            kefu_login($userId);
        }

        send_waiting_call($mysql, $userId);

        break;
    }

    case 101:
    {
        //连接数据库
        $mysql  = new mysqldb();
        $ret    = $mysql->open_db();
        if ($ret === FAILURE)
        {
            exit();
        }

        $userId  = $data['data']['userId'];
        $ret     = $mysql->update('ykj_app_user', ['online_status' => 0], 'platform_id='.$data['data']['userId']);
        if ($ret === FAILURE)
        {
            loge("update offline status failed: platform_id = ".$userId);

            break;
        }

        $ret     = $mysql->find('ykj_app_user', 'account_type,disturb_flag', 'platform_id='.$userId);
        if (($ret === FAILURE) || empty($ret))
        {
            loge("get user base info failed: platform_id = ".$userId);

            break;
        }

        $redis  = new RedisUtils(['select' => 6]);
        $redis->zrem('userOnline', $userId);

        $redis->select(7);
        $ret  = $redis->zrank("server:online", $userId);
        if ($ret !== false)
        {
            kefu_logout($userId);
        }
        
        break;
    }

    case 102:
    {
        $json  = ['drop' => (int)0, 'to' => [(int)$data['data']['to']]];
        $body  = xor_encode(json_encode($json), strlen($body));
        
        echo $body;

        break;
    }
    default:
    {
        break;
    }
}
